# test event sinks
use strict;

{
	package TestAppEventSink;
	our @ISA = qw(PlPN::AppEventSink);
	
	my $added;
	
	sub new {
		my $class = shift;
		my $self = $class->SUPER::new(@_);
		
		my $docs = PlPN->CurrentDocuments;
		if (@$docs) {
			if ($TestDocumentEventSink::docsink) {
				$docs->[0]->AddEventSink($TestDocumentEventSink::docsink);
			}
			if ($TestTextEditorEventSink::tesink) {
				$docs->[0]->AddEventSink($TestTextEditorEventSink::tesink);
			}
			$added = 1;
		}
	
		
		return $self;
	}

	sub OnNewDocument {
		my ($self, $doc) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Opening '$title' ($filename)");
		
		if (not $added) {
			if ($TestDocumentEventSink::docsink) {
				$doc->AddEventSink($TestDocumentEventSink::docsink);
			}
			if ($TestTextEditorEventSink::tesink) {
				$doc->AddEventSink($TestTextEditorEventSink::tesink);
			}
			$added = 1;
		}
	}

	sub OnAppClose {
		my ($self) = @_;
		PlPN->alert("Closing PN");
	}

	sub OnDocSelected {
		my ($self, $doc) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Activating '$title' ($filename)");
	}

	sub OnFirstEditorCreated {
		my ($self, $hWndScintilla) = @_;
		PlPN->alert("First editor created: $hWndScintilla");
	}
}

{
	package TestDocumentEventSink;
	our @ISA = qw(PlPN::DocumentEventSink);
	
	our $docsink;

	sub OnSchemeChange {
		my ($self, $doc, $scheme) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Changing scheme to '$scheme' for '$title' ($filename)");
	}

	sub OnDocClosing {
		my ($self, $doc) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Closing '$title' ($filename)");
	}

	sub OnAfterLoad {
		my ($self, $doc) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Done loading '$title' ($filename)");
	}

	sub OnBeforeSave {
		my ($self, $doc, $newfile) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("About to save '$title' ($filename) as '$newfile'");
	}

	sub OnAfterSave {
		my ($self, $doc, $newfile) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Saved '$title' ($filename)");
	}

	sub OnModifiedChanged {
		my ($self, $doc, $modified) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Modified changed to '$modified' for '$title' ($filename)");
	}

	sub OnWriteProtectChanged {
		my ($self, $doc, $writeProtect) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("WriteProtect changed to '$writeProtect'");
	}
}

{
	package TestTextEditorEventSink;
	our @ISA = qw(PlPN::TextEditorEventSink);
	
	our $tesink;

	sub OnCharAdded {
		my ($self, $doc, $char) = @_;
		my $title = $doc->GetTitle;
		my $filename = $doc->GetFileName;
		PlPN->alert("Char '$char' added to '$title' ($filename)");
	}
}

use PlPN qw(PN);

$TestDocumentEventSink::docsink = new TestDocumentEventSink;
$TestTextEditorEventSink::tesink = new TestTextEditorEventSink;
my $appsink = new TestAppEventSink;

PN->AddEventSink($appsink);
#PN->RemoveEventSink($appsink);
